package com.code.zg.rxapplication.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.code.zg.rxapplication.R;
import com.code.zg.rxapplication.adapter.APP2Adapter;
import com.code.zg.rxapplication.bean.User;
import com.code.zg.rxapplication.utils.SeApiManager;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Subscriber;

public class APP2Activity extends AppCompatActivity implements APP2Adapter.ViewHolder.OpenProfileListener {
    @BindView(R.id.so_recyclerview)
    RecyclerView mRecyclerView;

    @BindView(R.id.so_swipe)
    SwipeRefreshLayout mSwipe;
    private APP2Adapter mAdapter;
    private SeApiManager mSeApiManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app2);
        ButterKnife.bind(this);
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .showImageOnFail(R.drawable.ic_launcher)
                .showImageOnLoading(R.drawable.ic_launcher)
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .build();
        ImageLoader.getInstance().init(config);
        mAdapter = new APP2Adapter(new ArrayList<User>());
        mAdapter.setOpenProfileListener(this);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);

        mSeApiManager = new SeApiManager();

        mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshList();
            }
        });

        refreshList();
    }
    private void refreshList() {
        showRefresh(true);
        mSeApiManager.getMostPopularSOusers(10)
                .subscribe(new Subscriber<List<User>>() {
                    @Override
                    public void onCompleted() {
                        Log.i("xx", "onCompleted: ");
                    }

                    @Override
                    public void onError(Throwable e) {
//                        Timber.e(error.getMessage());
                        showRefresh(false);
                    }

                    @Override
                    public void onNext(List<User> users) {
                        showRefresh(false);
                        mAdapter.updateUsers(users);
                    }
                });
    }

    private void showRefresh(boolean show) {
        mSwipe.setRefreshing(show);
        int visibility = show ? View.GONE : View.VISIBLE;
        mRecyclerView.setVisibility(visibility);
    }

    @Override
    public void open(String url){
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
}
}
